package com.yunkaweilai.android.activity.operation.score;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.member.MemberHeadModel;
import com.yunkaweilai.android.model.member.MemberInfoModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * 积分日志
 * 白写 已经写好了的
 */
public class ScoreChangeAcitivity3 extends BaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate {

    @BindView(R.id.id_img_head)
    ImageView idImgHead;
    @BindView(R.id.id_tv_card_infor)
    TextView idTvCardInfor;
    @BindView(R.id.id_tv_name)
    TextView idTvName;
    private static final String SCORE_MEMBER_ID = "SCORE_MEMBER_ID";
    private String member_id;
    private MemberHeadModel model;

    private int currpage = 1;
    public static void actionStart(Context context, String member_id) {
        Intent intent = new Intent(context, ScoreChangeActivity2.class);
        intent.putExtra(SCORE_MEMBER_ID, member_id);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_change_acitivity3);
        ButterKnife.bind(this);
        member_id = getIntent().getStringExtra(SCORE_MEMBER_ID);
        initview();
    }

    private void initview() {
        new TitleBuilder(mAc).setLeftImage(R.id.ic_img_back).setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        }).setTitleText("积分日志");

        getMemberHead();
    }

    private void inithttp(){
        HttpRequest.postUrl(Api.APP_POINTS_LIST)
                .addParams("ShowPage","1")
                .addParams("PageSize","10")
                .addParams("CurrPage",currpage + "")
                .addParams("MemberId",member_id)
                .addParams("source","0")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {

                    }

                    @Override
                    public void onError(Exception e) {
                        showToast(e.getMessage());
                    }
                });
    }

    //获取头部会员信息
    private void getMemberHead() {
        HttpRequest.postUrl(Api.MEMBER_INFO_BASIC)
                .addParams("member_id", member_id)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean codeInfoRs = Utils.checkCode(mAc, json);
                        if (codeInfoRs) {
                            Gson gson = new Gson();
                            MemberInfoModel memberInfoModel = gson.fromJson(json, MemberInfoModel.class);
                            model = memberInfoModel.getData().getList();
                            updateHead(model);
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        showToast(e.getMessage());
                    }
                });
    }

    //设置头部会员信息
    private void updateHead(MemberHeadModel headModel) {
        Utils.MemberHeadImg(mAc, model.getMember_avatar(), model.getMember_sex(), idImgHead);
        idTvName.setText(headModel.getMember_name() + "(" + headModel.getLevel_name() + ": " + headModel.getCard_no() + ")");
        idTvCardInfor.setText("折扣：" + headModel.getDiscount() / 10 + "折   积分：" + headModel.getMember_points() + "   余额：" + headModel.getMember_price());
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {

    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        inithttp();
        return true;
    }
}
